#!/usr/bin/env python
from fdfs_client.client import Fdfs_client
from django.core.files.storage import Storage
from django.conf import settings
from django.utils.deconstruct import deconstructible


@deconstructible
class MyStorage(Storage):

    def _open(self, name, mode='rb'):
        pass

    def _save(self, name, content, max_length=None):
        client = Fdfs_client('utils/fastdfs/client.conf')

        data = content.read()

        result = client.upload_by_buffer(data)
        print(result)
        """
        {'Group name': 'group1', 
        'Remote file_id': 'group1/M00/00/00/wKgTgGC2_IuAA_k7AAaInSze-cQ113.jpg', 
        'Status': 'Upload successed.', 
        'Local file name': '/home/ubuntu/Desktop/images/01.jpg', 
        'Uploaded size': '418.00KB', 
        'Storage IP': '192.168.19.128'}
        """
        if result.get('Status') == 'Upload successed.':
            return result.get('Remote file_id')
        # 把file_id返回，因为　系统要使用

    def exists(self, name):
        pass

    def url(self, name):

        return settings.FDFS_URL + name



